package model

import (
	"pro-downloader/app/utils"
	"fmt"
	"gopkg.in/go-playground/validator.v8"
)

type Task struct {
	Url  string `form:"url" binding:"required"`
	Name string `form:"name" binding:"required"`
}

func (r *Task) GetError(err validator.ValidationErrors) string {
	if val, exist := err["Task.Url"]; exist {
		if val.Field == "Url" {
			switch val.Tag {
			case "required":
				return "下载地址不能为空!!"
			}
		}
	}
	if val, exist := err["Task.Name"]; exist {
		if val.Field == "Name" {
			switch val.Tag {
			case "required":
				return "文件名不能为空!!"
			}
		}
	}

	return "参数错误，请检查"
}

func UpdateTask(id int64, status int) {
	//更新状态
	res, err := utils.DbConn.Exec("update task set status=? where id=?", status, id)
	if err != nil {
		fmt.Println("exec failed, ", err)
		return
	}
	row, err := res.RowsAffected()
	if err != nil {
		fmt.Println("UpdateTask failed, ", err)
	}
	fmt.Println("更新",row,"状态:30")

}
